package com.tmpt.service.impl;

import com.tmpt.beans.A1TemplateTable;
import com.tmpt.dao.intf.IA1TemplateTableDao;
import com.tmpt.dao.intf.ITemplateDao;
import com.tmpt.dto.ConstantDto;
import com.tmpt.dto.Result;
import com.tmpt.others.db.DynamicDataSourceHolder;
import com.tmpt.service.ITemplateInterceptService;
import com.tmpt.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class TemplateInterceptService implements ITemplateInterceptService {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource private ITemplateDao templateBizDao;
	@Resource private ITemplateDao templateTemlDao;
	@Resource private IA1TemplateTableDao a1TemplateTableDao;



	/**
	 * 拦截器:向模板表中插入数据时的拦截器
	 * @param paramList
	 * @return
	 */
	@Override
	public Result interceptAddTemplateTable(List<Map<String,Object>> paramList){
		Result result = new Result();

		String dataSource = "";
		String tableName = "";
		for(Map<String,Object> map : paramList){
			String columnName = map.get("columnName").toString();
			String columnValue = map.get("columnValue").toString();
			if(columnName.equals("tableName")){
				tableName = columnValue;
			}
			if(columnName.equals("dataSource")){
				dataSource = columnValue;
			}
		}

		//加载数据源，向指定数据库create table
		if(StringUtils.isNull(dataSource)){
			dataSource = ConstantDto.defaultDataSource;
		}
		DynamicDataSourceHolder.setDataSource(dataSource);

		//判断表是否存在,不存在就像数据库里create table
		try{
			//.out.println("====tableName=="+tableName);
			templateTemlDao.createTableNotExist(tableName);
		}catch (Exception e) {
			e.printStackTrace();
			return result.fault("创建表失败");
		}

		//result.setRemark("return");
		result.setRemark("continue");
		return result.success();
	}

	/**
	 * 拦截器:向模板列中插入数据时的拦截器
	 * @param paramList
	 * @return
	 */
	@Override
	public Result interceptAddTemplateColumn(List<Map<String,Object>> paramList){
		//.out.println("----interceptAddTemplateColumn");
		Result result = new Result();

		String a1TemplateTable_id = "";
		String columnName1 = "";
		String column_dataType = "";
		String column_isNull = "";
		for(Map<String,Object> map : paramList){
			String columnName = map.get("columnName").toString();
			String columnValue = map.get("columnValue").toString();
			if(columnName.equals("columnName")){
				columnName1 = columnValue;
			}
			if(columnName.equals("a1TemplateTable_id")){
				a1TemplateTable_id = columnValue;
			}
			if(columnName.equals("column_dataType")){
				column_dataType = columnValue;
			}
			if(columnName.equals("column_isNull")){
				column_isNull = columnValue;
			}
		}
		//is null
		if(!StringUtils.isNull(column_isNull) && column_isNull.equals("1")){
			column_isNull = "not null";
		}else{
			column_isNull = "null";
		}

		if(StringUtils.isNull(column_dataType)){
			return result.fault("列类型为空，请选择："+column_dataType);
		}

		//取table name
		A1TemplateTable templateTable = a1TemplateTableDao.fetch(Long.parseLong(a1TemplateTable_id));

		//
		result = alterTableAddColumn(templateTable,columnName1,column_dataType,column_isNull);

		return result;
	}

	/**
	 * 向数据库表中 alter table add column ,by datasource select
	 * @param templateTable
	 * @param columnName1
	 * @param column_dataType
	 * @param column_isNull
	 * @return
	 */
	public Result alterTableAddColumn(A1TemplateTable templateTable,String columnName1, String column_dataType, String column_isNull ){
		//.out.println("----alterTableAddColumn");
		Result result = new Result();

		String tableName = templateTable.getTableName();
		String dataSource = templateTable.getDataSource();

		//判断表是否存在,不存在就像数据库里create table
		try{
			//.out.println("====columnName==" + tableName + "--" + columnName1 + "--" + column_dataType + "--" + column_isNull);
			if(dataSource!=null && dataSource.equals(ConstantDto.bizDataSource)){
				templateBizDao.alterColumnNotExist(tableName, columnName1, column_dataType, column_isNull);
			}else{
				templateTemlDao.alterColumnNotExist(tableName, columnName1, column_dataType, column_isNull);
			}

		}catch (Exception e) {
			e.printStackTrace();
			return result.fault("创建列失败");
		}

		result.setRemark("continue");
		//result.setRemark("return");

		return result.success();
	}

}
